<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>声明周期</title>
</head>
<body>
    <button>来个看图神器</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{

            state = {
                url: 'http://api.xiaohigh.com/image/random',
            }

            render(){
                return <div>
                        <img width="500" src={this.state.url} onClick={this.handleClick} alt="" />
                    </div>
            }

            handleClick = () => {
                this.setState({
                    url: 'http://api.xiaohigh.com/image/random?t='+Math.random(),
                })   
            }

            //组件挂载完毕回调
            componentDidMount(){
                console.log('组件完成了挂载');
            }

            //组件将要卸载
            componentWillUnmount(){
                console.log('组件将要卸载');
            }
        }

        let btns = document.querySelectorAll('button');

        //绑定事件
        btns[0].onclick = function(){
            //渲染虚拟 DOM
            ReactDOM.render(<App />, document.querySelector("#root"));
        }

        btns[1].onclick = function(){
            ReactDOM.unmountComponentAtNode(document.querySelector('#root'));
        }


    </script>
</body>
</html>